package be.donovan.gui.helper.components;

import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Vector;

import javax.swing.text.NumberFormatter;

import org.jdesktop.swingx.JXTable;
import org.springframework.util.ReflectionUtils;

import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;



public class EditableMVCTableModel<T> extends MVCTableModel<T> {

	protected Vector<String> propertySetterNames;

	protected List<T> toDelete;

	protected List<T> toAdd;
	
	protected AbstractController<ViewI> controller;

	public EditableMVCTableModel(Collection<T> data, Vector<String> propertyGetterNames, Vector<String> propertySetterNames,
			Vector<String> displayNames, AbstractController controller) {
		super(data, propertyGetterNames, displayNames);
		this.propertySetterNames = propertySetterNames;
		this.controller = controller;
	}

	@Override
	public boolean isCellEditable(int row, int column) {
		return true;
	}

	public T fromRowVector(int rowIndex) {
		return dataMap.get(rowMapping.get(rowIndex));
	}

	public Collection<T> getData() {
		List<T> result = new ArrayList<T>();
		for (int row = 0; row < getRowCount(); row++) {
			result.add(fromRowVector(row));
		}
		return result;
	}

	@Override
	public void setValueAt(Object value, int row, int column) {
		T dto = dataMap.get(rowMapping.get(row));
		Method m = ReflectionUtils.findMethod(dto.getClass(), propertySetterNames.get(column), new Class[] { columnClasses
				.get(column) });
		try {
			ReflectionUtils.makeAccessible(m);
			System.out.println(value);
			if(columnClasses
				.get(column).equals(Double.class)){
				
				Double dValue = new Double(value.toString().replace(',', ((DecimalFormat)DecimalFormat.getInstance()).getDecimalFormatSymbols().getDecimalSeparator()));
				m.invoke(dto,dValue);
				super.setValueAt(dValue, row, column);
			}else{
				m.invoke(dto, value);
				super.setValueAt(value, row, column);
			}
			
		} catch (Exception e) {
				e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		new Double("12,2");
	}
}
